/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author            Notes
 * 2021-01-03     iysheng           first version
 */

//#include "drv_comm.h"
#include <drivers/adc.h>
#include "drv_gpio.h"
#include "gd32f30x.h"
#define DBG_TAG             "drv.adc"
#define DBG_LVL             DBG_INFO

#include <rtdbg.h>

#ifdef RT_USING_ADC

#define MAX_EXTERN_ADC_CHANNEL    10

typedef struct {
    struct rt_adc_device adc_dev;
    char name[8];
    rt_base_t adc_pins[10];
    void *private_data;
} gd32_adc_device;

static gd32_adc_device g_gd32_devs[] = {
#ifdef BSP_USING_ADC0
    {
        {0},
        "adc",
        {
            GET_PIN(A, 0), GET_PIN(A, 1), GET_PIN(A, 2), GET_PIN(A, 3),
            GET_PIN(A, 4), GET_PIN(A, 5), GET_PIN(A, 6), GET_PIN(A, 7),
            GET_PIN(B, 0), GET_PIN(B, 1),
        },
        0,
    },
#endif

#ifdef BSP_USING_ADC1
    {
        {},
        "adc1",
        {
            GET_PIN(A, 0), GET_PIN(A, 1), GET_PIN(A, 2), GET_PIN(A, 3),
            GET_PIN(A, 4), GET_PIN(A, 5), GET_PIN(A, 6), GET_PIN(A, 7),
            GET_PIN(B, 0), GET_PIN(B, 1), GET_PIN(C, 0), GET_PIN(C, 1),
            GET_PIN(C, 2), GET_PIN(C, 3), GET_PIN(C, 4), GET_PIN(C, 5),
        },
        ADC1,
    },
#endif
};

/*
 * static void init_pin4adc
 *
* 初始化指定的管腳为 analog 模式
 * @ rt_uint32_t pin: pin information
 * return: N/A
 */
static void init_pin4adc(rt_base_t pin)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};
		
    GPIO_InitStruct.Pin  = PIN_GDPIN(pin);
   // GPIO_InitStruct.Pull = GPIO_PUPD_NONE;
    GPIO_InitStruct.Speed = GPIO_OSPEED_50MHZ;
		GPIO_InitStruct.Mode = GPIO_MODE_AIN ;
		
		gpio_init(PIN_GDPORT(pin), GPIO_InitStruct.Mode, GPIO_InitStruct.Speed , PIN_GDPIN(pin));
}

static rt_err_t gd32_adc_enabled(struct rt_adc_device *device, rt_uint32_t channel, rt_bool_t enabled)
{
    gd32_adc_device * gd32_adc = (gd32_adc_device *)device;
    if (channel >= MAX_EXTERN_ADC_CHANNEL)
    {
        LOG_E("invalid channel");
        return -RT_ERROR;
    }
    if (enabled == ENABLE)
    {
				init_pin4adc(gd32_adc->adc_pins[channel]);
				/* ADC SCAN function enable */
    adc_special_function_config(ADC0, ADC_SCAN_MODE, ENABLE);
    adc_special_function_config(ADC0, ADC_CONTINUOUS_MODE, DISABLE); 
    /* ADC trigger config */
    adc_external_trigger_source_config(ADC0, ADC_INSERTED_CHANNEL, ADC0_1_2_EXTTRIG_INSERTED_NONE);
    /* ADC mode config */
    adc_mode_config(ADC_MODE_FREE);
    /* ADC data alignment config */
    adc_data_alignment_config(ADC0, ADC_DATAALIGN_RIGHT);  
    /* ADC channel length config */
    adc_channel_length_config(ADC0, ADC_INSERTED_CHANNEL, 2);

  
    /* ADC temperature sensor channel config */
    adc_inserted_channel_config(ADC0, 0, ADC_CHANNEL_16, ADC_SAMPLETIME_239POINT5);
    /* ADC internal reference voltage channel config */
    adc_inserted_channel_config(ADC0, 1, ADC_CHANNEL_17, ADC_SAMPLETIME_239POINT5);

    adc_external_trigger_config(ADC0, ADC_INSERTED_CHANNEL, ENABLE);
  
    /* ADC temperature and Vrefint enable */
    adc_tempsensor_vrefint_enable();
 
    
    /* enable ADC interface */
    adc_enable(ADC0);
		rt_thread_delay(1);
		  /* ADC calibration and reset calibration */
    adc_calibration_enable(ADC0);
    }
    else
    {
       adc_disable(ADC0);
    }

    return 0;
}

static rt_err_t gd32_adc_convert(struct rt_adc_device *device, rt_uint32_t channel, rt_uint32_t *value)
{
   // ADC_TypeDef *ADCx;

    if (!value)
    {
        LOG_E("invalid param");
        return -RT_EINVAL;
    }
     adc_software_trigger_enable(ADC0,ADC_REGULAR_CHANNEL);
		 /* wait the end of conversion flag */
    while(!adc_flag_get(ADC0,ADC_FLAG_EOC))
		{
		    rt_thread_mdelay(1);
		}
    /* clear the end of conversion flag */
    adc_flag_clear(ADC0,ADC_FLAG_EOC);
    //ADCx = (ADC_TypeDef *)(device->parent.user_data);
    *value = adc_regular_data_read(ADC0);//ADC_GetConversionValue(ADCx);

    return 0;
}

static struct rt_adc_ops g_gd32_adc_ops = {
    gd32_adc_enabled,
    gd32_adc_convert,
};

static int rt_hw_adc_init(void)
{
    int ret, i = 0;
#ifdef BSP_USING_ADC0
    /* enable GPIOA clock */
   /* enable ADC clock */
    rcu_periph_clock_enable(RCU_ADC0);
    /* config ADC clock */
    rcu_adc_clock_config(RCU_CKADC_CKAPB2_DIV4);
#endif
#ifdef BSP_USING_ADC1
    rcu_periph_clock_enable(RCU_ADC1);
#endif

    //for (i=0; i < 3;i++)//ARRAY_SIZE(g_gd32_devs); i++)
    {
			i = 0;
        ret = rt_hw_adc_register(&g_gd32_devs[i].adc_dev, \
            (const char *)g_gd32_devs[i].name, \
            &g_gd32_adc_ops, g_gd32_devs[i].private_data);
        if (ret != RT_EOK)
        {
            /* TODO err handler */
            LOG_E("failed register %s, err=%d", g_gd32_devs[i].name, ret);
        }
    }
    return ret;
}
INIT_BOARD_EXPORT(rt_hw_adc_init);
#endif
